package com.sec.foodit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class DataManager {

	private static DataManager instance = null;

	private ArrayList<Restaurant> fList;
	private ArrayList<Restaurant> rList;
	public ArrayList<Post> pList;
	
	private String fStr;
	String path;
	
	public String curRestName = null;

	public DataManager()
	{
		fList = new ArrayList<Restaurant>();
		rList = new ArrayList<Restaurant>();
		pList = new ArrayList<Post>();
	}

	public static DataManager getInstance()
	{
		if(instance == null)
			instance = new DataManager();
		return instance;
	}

	public void loadFile(Context context)
	{
		try
		{
			File file = context.getDir("resources", Context.MODE_PRIVATE);
			path = file.getAbsolutePath();
			Log.e("foodit","path: " + path);
			AssetManager as = context.getAssets();
			String[] list = as.list("resources");
			Log.e("foodit","asset list: " + Arrays.toString(list));

			for(String k : list)
			{
				Log.e("foodit","file copy: " + k);
				InputStream is = as.open("resources/"+k);
				FileOutputStream os = new FileOutputStream(path + "/" + k);

				int bytesRead = 0;
				byte[] buffer = new byte[1024];   
				while ((bytesRead = is.read(buffer, 0, 1024)) != -1)
				{
					os.write(buffer, 0, bytesRead);
				}
				is.close();
				os.close();
			}
			Log.e("foodit","file copy completed");

			FileReader fr = new FileReader(path + "/asset.json");
			BufferedReader br = new BufferedReader(fr);

			String result = new String();
			String tmp = null;
			while((tmp = br.readLine())!=null)
				result += tmp;

			JSONObject obj = new JSONObject(result);
			loadRestaurantFromJson(obj.getJSONArray("restaurant"));
			loadFavoriteFromJson(obj.getJSONArray("favorite"));
		}
		catch(Exception e)
		{
			Log.e("foodit","## DataManager::loadFile::Error: " + e.toString());
		}
	}

	private void loadRestaurantFromJson(JSONArray arr)
	{
		try
		{
			rList.clear();
			for(int i=0; i<arr.length(); i++)
			{
				JSONObject obj = arr.getJSONObject(i);
				rList.add(new Restaurant(obj.getString("name"), 
						obj.getString("photo"), 
						obj.getString("loc"),
						obj.getLong("point"),
						obj.getString("intro")));
			}
		}
		catch(Exception e)
		{
			Log.e("foodit","##DataManager::loadRestaurantFromJson::Error: " + e.toString());
		}
	}

	private void loadFavoriteFromJson(JSONArray arr)
	{
		try
		{
			fList.clear();
			for(int i=0; i<arr.length(); i++)
			{
				JSONObject obj = arr.getJSONObject(i);
				for(int j=0; j<rList.size(); j++)
				{
					if(obj.getString("name").equals(rList.get(j).getName()))
						fList.add(rList.get(j));
				}
			}
		}
		catch(Exception e)
		{
			Log.e("foodit","##DataManager::loadFavoriteFromJson::Error: " + e.toString());
		}
	}



	public ArrayList<Restaurant> getfList() {
		return fList;
	}

	public void setfList(ArrayList<Restaurant> fList) {
		this.fList = fList;
	}

	public String getfStr() {
		return fStr;
	}

	public void setfStr(String fStr) {
		this.fStr = fStr;
	}

	public Bitmap getBitmapFromPath(String imgPath)
	{
		imgPath = path + "/" + imgPath;

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imgPath, options);
		options.inSampleSize = calculateInSampleSize(options, 80, 80);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(imgPath, options);

	}

	/* 비트맵 샘플링 사이즈 결정 */
	public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
	{    
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) 
		{	
			final int heightRatio = Math.round((float) height / (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
		}
		return inSampleSize;
	}
}